package net.taiqin.tqerp.service.impl.po;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.taiqin.tqerp.common.CommonUtils;
import net.taiqin.tqerp.controller.param.po.PurchaseParam;
import net.taiqin.tqerp.dao.po.IPurchaseDao;
import net.taiqin.tqerp.domain.common.BaseVO;
import net.taiqin.tqerp.domain.po.POHeaderVO;
import net.taiqin.tqerp.domain.po.POLinesVO;
import net.taiqin.tqerp.domain.po.POMessagesVO;
import net.taiqin.tqerp.domain.po.POReturnLinesVO;
import net.taiqin.tqerp.domain.po.POStatusCountsVO;
import net.taiqin.tqerp.service.po.IPurchaseService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
public class PurchaseServiceImpl implements IPurchaseService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IPurchaseDao purchaseDao;

    public List<POStatusCountsVO> findPoStatusCounts(PurchaseParam parm) {
        return this.purchaseDao.findPoStatusCounts(parm);
    }

    public PageInfo<POHeaderVO> findPOHeaderList(PurchaseParam parm) {
        PageHelper.startPage(parm.getPageNum(), parm.getPageSize());
        List<POHeaderVO> poHeaderVOList = this.purchaseDao.findPOHeaderList(parm);
        return new PageInfo(poHeaderVOList);
    }

    public PageInfo<POLinesVO> findPODetailList(PurchaseParam parm) {
        PageHelper.startPage(parm.getPageNum(), parm.getPageSize());
        List<POLinesVO> poLinesVOList = this.purchaseDao.findPODetailList(parm);
        return new PageInfo(poLinesVOList);
    }

    public PageInfo<POMessagesVO> findPOMessages(PurchaseParam parm) {
        PageHelper.startPage(parm.getPageNum(), parm.getPageSize());
        List<POMessagesVO> poMessagesVOList = this.purchaseDao.findPOMessages(parm);
        return new PageInfo(poMessagesVOList);
    }

    public PageInfo<POReturnLinesVO> findPOReturnLines(PurchaseParam parm) {
        PageHelper.startPage(parm.getPageNum(), parm.getPageSize());
        List<POReturnLinesVO> poMessagesVOList = this.purchaseDao.findPOReturnLines(parm);
        return new PageInfo(poMessagesVOList);
    }

    public BaseVO updatePo(PurchaseParam parm) {
        if (parm == null) {
            return CommonUtils.failed("参数无效!");
        } else if (!StringUtils.equalsAnyIgnoreCase(parm.getUserType(), new CharSequence[]{"CUSTOMER", "VENDOR"})) {
            return CommonUtils.failed("用户类型无效，必须为CUSTOMER/VENDOR!");
        } else if (!StringUtils.equalsAnyIgnoreCase(parm.getActionType(), new CharSequence[]{"ACCEPT", "UPDATE", "MESSAGE"})) {
            return CommonUtils.failed("操作类型无效，必须为ACCEPT/UPDATE/MESSAGE!");
        } else {
            try {
                if ("CUSTOMER".equalsIgnoreCase(parm.getUserType())) {
                    if (StringUtils.equalsAnyIgnoreCase(parm.getActionType(), new CharSequence[]{"ACCEPT", "UPDATE"})) {
                        this.purchaseDao.doCustomerUpdate(parm);
                        return CommonUtils.succsesed();
                    }
                } else if ("VENDOR".equalsIgnoreCase(parm.getUserType())) {
                    if ("UPDATE".equalsIgnoreCase(parm.getActionType())) {
                        this.purchaseDao.doVendorUpdate(parm);
                        return CommonUtils.succsesed();
                    }

                    if ("ACCEPT".equalsIgnoreCase(parm.getActionType())) {
                        if (CollectionUtils.isEmpty(parm.getPoLinesList())) {
                            Map<String, Object> map = new HashMap();
                            map.put("poHeaderId", parm.getPoHeaderId());
                            map.put("userId", parm.getUserId());
                            this.purchaseDao.doVendorAcceptAll(map);
                        } else {
                            for(POLinesVO poLinesVO : parm.getPoLinesList()) {
                                Map<String, Object> map = new HashMap();
                                map.put("shipmentState", poLinesVO.getShipmentState());
                                map.put("poLocationId", poLinesVO.getPoLocationId());
                                map.put("userId", parm.getUserId());
                                this.purchaseDao.doVendorAccept(map);
                            }
                        }

                        return CommonUtils.succsesed();
                    }
                }

                if ("MESSAGE".equalsIgnoreCase(parm.getActionType())) {
                    if (CollectionUtils.isEmpty(parm.getPoLinesList())) {
                        return CommonUtils.failed("请指定修改采购单行!");
                    }

                    for(POLinesVO poLinesVO : parm.getPoLinesList()) {
                        Map<String, Object> map = new HashMap();
                        map.put("poLocationId", poLinesVO.getPoLocationId());
                        map.put("poMessge", parm.getPoMessge());
                        map.put("orgId", parm.getOrgId());
                        map.put("userId", parm.getUserId());
                        this.purchaseDao.sendPoMessage(map);
                    }

                    return CommonUtils.succsesed();
                }
            } catch (Exception ex) {
                return CommonUtils.failed("操作失败，错误信息：" + ex.toString());
            }

            return CommonUtils.succsesed();
        }
    }
}
